﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using DataStorage;

namespace Booky.WinFormsClient
{
    public partial class AddEditTag : Form
    {
        AsyncFunc<string, string> addEditTagTask;

        public Tag ExistingTag { get; set; }

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
            int nTopRect, // y-coordinate of upper-left corner
            int nRightRect, // x-coordinate of lower-right corner
            int nBottomRect, // y-coordinate of lower-right corner
            int nWidthEllipse, // height of ellipse
            int nHeightEllipse // width of ellipse
        );

    //    In Constructor of the form you need to call this after InitializeComponent()

      //  Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 15, 15));

        public AddEditTag()
        {
            InitializeComponent();
            addEditTagTask = new AsyncFunc<string,string>(AddEditTagTask);
            addEditTagTask.Completed += new AsyncFuncCompletedEventHandler<string>(addEditTagTask_Completed);
            //getAnswer.ProgressChanged += (s, a) => progressBar1.Value = a.ProgressPercentage;
            //this.FormBorderStyle = 
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 0, 0));
        }

        public void DataBind()
        {
            if (this.ExistingTag != null)
            {
                txtTitle.Text = this.ExistingTag.Name;
                this.Text = string.Format(this.Text, "Edit");
            }
            else
            {
                this.Text = string.Format(this.Text,"Add");
            }
        }

        void addEditTagTask_Completed(object sender, AsyncFuncCompletedEventArgs<string> e)
        {
            if (e.Error != null)
            {
                // Report exception
            }
            else if (e.Cancelled)
            {
                // Ignore
                MessageBox.Show("Operation cancelled");
            }
            else
            {
            //    textBox1.Text = e.Result;
                //close the window
                this.Close();
                
                //we should be inserting a row to the list control here rather than updating the entire listcontrol
            }

            //progressBar1.Value = 0;
        }

        public MainForm OwnerForm
        {
            get
            {
                return (MainForm)this.Owner;
            }
        }

        private string AddEditTagTask(string question, Func<bool> isCancelled, Action<int> reportProgress)
        {
            Repository r = new Repository();

            if(ExistingTag == null)
            {
                Tag t = new Tag();
                t.Name = txtTitle.Text ?? "";
                t.CreatedDate = DateTime.Now;
               
                //Update the bookmark list control with our updated
                //OwnerForm.InsertBookmarkToListControl(b);
                r.InsertTagLocalCloud(txtTitle.Text ?? "");//UpdateBookmark(txtTitle.Text ?? "", txtLocation.Text ?? "");
            }
            else
            {
                Tag newTag = new Tag();
                newTag.Id = ExistingTag.Id;
                newTag.Name = txtTitle.Text;
                newTag.LastUpdated = DateTime.Now;
                r.UpdateTagLocalCloud(newTag);
            }
            
            return "42";
        }

        private void AddEditTagButton_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(errorProvider1.GetError(txtTitle)) )
                addEditTagTask.TryInvokeAsync("What is the Answer to the Ultimate Question of Life, the Universe and Everything");
        }

        private void txtTitle_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtTitle.Text))
            {
                this.errorProvider1.SetError(txtTitle, "Title is required");
            }
            else
            {
                errorProvider1.SetError(txtTitle, "");
            }
        }

      
    }
}
